package mest.service.cxj.impl;

import mest.dao.cxj.ModelFileDao;
import mest.entity.model_file;
import mest.service.cxj.ModelFileService;
import mest.service.cxj.ModelingDictionariesService;
import mest.service.system.DBService;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor=Exception.class)
public class ModelFileServiceImpl implements ModelFileService {

    @Resource
    private ModelFileDao modelFileDao;
    @Autowired
    private ModelingDictionariesService modelingDictionariesService;
    @Autowired
    private DBService dbService;

    @Override
    public List<model_file> selectAllModelFile(Integer page, Integer limit, String modelname,String theirDb) {
        return modelFileDao.selectAllModelFile((page - 1) * limit, limit, modelname,theirDb);
    }

    @Override
    public Integer selectCountModelFile(String modelname,String theirDb) {
        return modelFileDao.selectCountModelFile(modelname,theirDb);
    }

    @Override
    public ResultObj insertModelFile(model_file modelFile,String theirDb) {
        try {
            int num = modelFileDao.insertModelFile(modelFile,theirDb);
            if (num > 0) {
                modelingDictionariesService.updateModelingDictionariesByModelFileNo(theirDb); //更新工序档案流水号
                return  ResultObj.createSuccessMsg(ResultObj.INSERT_SUCCESS);
            }
        }catch (Exception e){
            // TODO: handle exception
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                return ResultObj.createErrorMsg("编码重复");
            }
            return ResultObj.createErrorMsg(ResultObj.ERROR);
        }


        return ResultObj.createErrorMsg(ResultObj.ERROR);
    }

    @Override
    public boolean modifyModelFile(model_file modelFile,String theirDb) {
        boolean flag = false;
        int num = modelFileDao.modifyModelFile(modelFile,theirDb);
        if (num > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteModelFile(Integer id,String theirDb) {
        boolean flag = false;
        int num = modelFileDao.deleteModelFile(id,theirDb);
        if (num > 0) {
            flag = true;
        }
        return flag;
    }


}
